#include "formslaveidsetting.h"
#include "ui_formslaveidsetting.h"
#include "dcsproto/proto.h"

#include <QMessageBox>
#include <QInputDialog>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QSpinBox>

#include <QDebug>
#include <global.h>

FormSlaveIDSetting::FormSlaveIDSetting(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::FormSlaveIDSetting)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags() | Qt::Dialog);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_DeleteOnClose);

    mDbopt = new DCSDbOperator();
    quint8 iface;
    if (!DCSDbOperator::getMasterParameterInterface(iface))
    {
        qDebug() << "db get interface error,set to default";
        iface = UDP_IFACE_EN_WAN;
    }

    if (iface == UDP_IFACE_EN_WAN)
    {
        mUdp = new UdpWorker(INTERFACE_WAN_NAME);
    }
    else
    {
        mUdp = new UdpWorker(INTERFACE_LAN_NAME);
    }

    qDebug() << "[FormSlaveIDSetting] \t Constructor";
}

FormSlaveIDSetting::~FormSlaveIDSetting()
{
    delete ui;
    delete mUdp;
    delete mDbopt;
    qDebug() << "[FormSlaveIDSetting] \t Destructor";
}

void FormSlaveIDSetting::on_pushButtonReturn_clicked()
{
    close();
}

/**
 * @brief FormSlaveIDSetting::on_pushButtonFirstFix_clicked
 * 首台分控ID固定
 */
void FormSlaveIDSetting::on_pushButtonFirstFix_clicked()
{
    bool ok;
    int firstid;
    firstid = QInputDialog::getInt(this, tr("设置"), tr("设置首台分控ID"), 1, 1, 256, 1, &ok);
    qDebug() << "set first slave id ok:" << ok;
    qDebug() << "firstid:" << firstid;
    if (ok)
    {
        char buf[60];
        memset(buf, 0x0, 60);
        struct slave_id_set_pdu pdu;
        memset(&pdu, 0x0, sizeof(pdu));
        pdu.type = EN_SLAVE_ID_SET_TYPE_FIRST_FIX;
        pdu.param.fist_fix_para.id = firstid;

        aklt_proto_encode((unsigned char *) buf, TYP_SLAVE_BROADCAST_OCCA_SET, CMD_SLAVE_ID_CONF_OCCA_SET, 0x0000,
                          (unsigned char *) &pdu, sizeof(pdu), SLAVE_BROADCAST);

        mUdp->dataSend(buf, 60);
    }

}

/**
 * @brief FormSlaveIDSetting::on_pushButtonAllFix_clicked
 * 固定所有分控ID
 */
void FormSlaveIDSetting::on_pushButtonAllFix_clicked()
{
    int ret;
    ret = QMessageBox::question(this, tr("提示"), tr("该操作会将所有分控ID固定，是否继续？"), QMessageBox::Yes | QMessageBox::No,
                                QMessageBox::No);
    qDebug() << "fix all slave id? ret:" << ret;
    if (ret == QMessageBox::Yes)
    {
        char buf[60];
        memset(buf, 0x0, 60);
        struct slave_id_set_pdu pdu;
        memset(&pdu, 0x0, sizeof(pdu));
        pdu.type = EN_SLAVE_ID_SET_TYPE_ALL_FIX;

        aklt_proto_encode((unsigned char *) buf, TYP_SLAVE_BROADCAST_OCCA_SET, CMD_SLAVE_ID_CONF_OCCA_SET, 0x0000,
                          (unsigned char *) &pdu, sizeof(pdu), SLAVE_BROADCAST);

        mUdp->dataSend(buf, 60);
    }
}

/**
 * @brief FormSlaveIDSetting::on_pushButtonClean_clicked
 * 清除所有分控ID
 */
void FormSlaveIDSetting::on_pushButtonClean_clicked()
{
    int ret;
    ret = QMessageBox::question(this, tr("提示"), tr("该操作会将所有分控ID清除，是否继续？"), QMessageBox::Yes | QMessageBox::No,
                                QMessageBox::No);
    qDebug() << "clean all slave id? ret:" << ret;
    if (ret == QMessageBox::Yes)
    {
        char buf[60];
        memset(buf, 0x0, 60);
        struct slave_id_set_pdu pdu;
        memset(&pdu, 0x0, sizeof(pdu));
        pdu.type = EN_SLAVE_ID_SET_TYPE_CLEAN;

        aklt_proto_encode((unsigned char *) buf, TYP_SLAVE_BROADCAST_OCCA_SET, CMD_SLAVE_ID_CONF_OCCA_SET, 0x0000,
                          (unsigned char *) &pdu, sizeof(pdu), SLAVE_BROADCAST);

        mUdp->dataSend(buf, 60);
    }
}

/**
 * @brief FormSlaveIDSetting::on_pushButtonReboot_clicked
 * 重启所有分控
 */
void FormSlaveIDSetting::on_pushButtonReboot_clicked()
{
    int ret;
    ret = QMessageBox::question(this, tr("提示"), tr("该操作会将所有分控重启，是否继续？"), QMessageBox::Yes | QMessageBox::No,
                                QMessageBox::No);
    qDebug() << "reboot all slave? ret:" << ret;
    if (ret == QMessageBox::Yes)
    {
        //send reboot cmd
    }
}

/**
 * @brief FormSlaveIDSetting::on_pushButtonModify_clicked
 * 修改指定分控ID
 */
void FormSlaveIDSetting::on_pushButtonModify_clicked()
{
    QDialog dialog(this);
    QFormLayout form(&dialog);
    // 原ID
    QString srcid = QString(tr("原ID"));
    QSpinBox *spinbox_srcid = new QSpinBox(&dialog);
    spinbox_srcid->setRange(1, 256);
    form.addRow(srcid, spinbox_srcid);

    //设置行间距
    form.setSpacing(30);

    // 更改ID
    QString dstid = QString(tr("更改为"));
    QSpinBox *spinbox_dstid = new QSpinBox(&dialog);
    spinbox_dstid->setRange(1, 256);
    form.addRow(dstid, spinbox_dstid);

    // Add Cancel and OK button
    QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);
    form.addRow(&buttonBox);
    QObject::connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
    QObject::connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));

    // Process when OK button is clicked
    if (dialog.exec() == QDialog::Accepted)
    {
        // Do something here
        qDebug() << "commited";
        qDebug() << "srcid" << spinbox_srcid->value();
        qDebug() << "dstid" << spinbox_dstid->value();
        int srcid = spinbox_srcid->value();
        int dstid = spinbox_dstid->value();
        if (srcid != dstid)
        {
            char buf[60];
            memset(buf, 0x0, 60);
            struct slave_id_set_pdu pdu;
            memset(&pdu, 0x0, sizeof(pdu));
            pdu.type = EN_SLAVE_ID_SET_TYPE_SPEC;
            pdu.param.spec_para.oldid = srcid;
            pdu.param.spec_para.newid = dstid;

            aklt_proto_encode((unsigned char *) buf, TYP_SLAVE_BROADCAST_OCCA_SET, CMD_SLAVE_ID_CONF_OCCA_SET, 0x0000,
                              (unsigned char *) &pdu, sizeof(pdu), SLAVE_BROADCAST);

            mUdp->dataSend(buf, 60);
        }
    }
}
